package com.leetcode.June;

/**
 * @Description: 合并二个链表成有序链表
 * @Author: Mr.Fxy
 * @CreateTime: 2024-06-28 18:02
 */
// class ListNode {
//      int val;
//      ListNode next;
//     ListNode(int x) { val = x; }
// }


public class list {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null) {
            return l2;
        }
        if(l2 == null) {
            return l1;
        }

        if(l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }

    public static void main(String[] args) {
        ListNode l1Node1 = new ListNode(1);
        ListNode l1Node2 = new ListNode(3);
        ListNode l1Node3 = new ListNode(5);
        l1Node1.next = l1Node2;
        l1Node2.next = l1Node3;

        ListNode l2Node1 = new ListNode(2);
        ListNode l2Node2 = new ListNode(4);
        ListNode l2Node3 = new ListNode(6);
        l2Node1.next = l2Node2;
        l2Node2.next = l2Node3;

        // 合并链表
        ListNode mergedList = mergeTwoLists(l1Node1, l2Node1);
    }

}
